# **************************************
# --*-- coding: utf-8 --*--
# @Time    : 2024-11-11
# @Author  : white
# @FileName: my_computer_dir.py
# @Software: PyCharm
# **************************************

import winreg
from tkinter import Button, \
    Variable, DISABLED, Radiobutton, Label
from .option_reg import Reg
from .base import Base
from comon import logger, Rows


class MyComputerDir(Base):
    def __init__(self, ui):
        super().__init__()
        self.ui = ui
        self.left_frame = self.ui.left_frame
        self.right_frame = self.ui.right_frame
        self.name = "此电脑下子文件夹"

        self.keys = {
            "视频"  : ["video", "{f86fa3ab-70d2-4fc7-9c99-fcbf05467f3a}"],
            "文档"  : ["document", "{d3162b92-9365-467a-956b-92703aca08af}"],
            "音乐"  : ["music", "{3dfdf296-dbec-4fb4-81d1-6a3438bcf4de}"],
            "下载"  : ["download", "{088e3905-0323-4b02-9826-5d99428e115f}"],
            "图片"  : ["picture", "{24ad3ad4-a569-4530-98e1-ab02f9417aa8}"],
            "3D对象": ["three_dim", "{0DB7E03F-FC29-4DC6-9020-FF41B59E513A}"],
            "桌面"  : ["desk_top", "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"],
        }
        self.data_type = winreg.REG_DWORD
        self.reg_path = r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace"
        self.reg_obj = Reg(self.reg_path)
        self.data = 1

        self.ui_init()

    def reg(self):
        for value in self.keys.values():
            tmp_result = getattr(self, value[0]).get()
            if tmp_result == "yes":
                self.reg_obj.create_dir_key(value[1])
            elif value[2] == "exist":
                self.reg_obj.del_dir_key(value[1])

    def ui_init(self):
        self._button()
        self.button.bind("<Button-1>", self.option)

    def _button(self):
        self.button = Button(self.left_frame, text=self.name)
        self.button.pack(pady=self.ui.pady_upper, anchor="w")

    @Base.clear_frame
    def option(self, event=None):
        if self.button['state'] == DISABLED:
            return
        if not self.is_admin():
            Label(self.right_frame, text="此功能需要管理员权限启动！\n请使用管理权限启动后操作！") \
                .pack()
            return

        row = Rows()
        for key, value in self.keys.items():
            if self.reg_obj.read_dir_key(value[1]):
                setattr(self, value[0], Variable(value="yes"))
                value.append("exist")
            else:
                setattr(self, value[0], Variable(value="no"))
                value.append("no")

            Label(self.right_frame, text=key).grid(row=row.get_row, column=0, sticky="w")
            Radiobutton(self.right_frame, text="不显示", variable=getattr(self, value[0]), value="no") \
                .grid(row=row.row, column=1, sticky="w")
            Radiobutton(self.right_frame, text="显示", variable=getattr(self, value[0]), value="yes") \
                .grid(row=row.row, column=2, sticky="w")

        self.all_show = Variable(value="-")
        Radiobutton(self.right_frame, text="全不显示", variable=self.all_show, value="no",
                    command=lambda: self._all_selected("no")) \
            .grid(row=row.get_row, column=0, sticky="w")
        Radiobutton(self.right_frame, text="全显示", variable=self.all_show, value="yes",
                    command=lambda: self._all_selected("yes")) \
            .grid(row=row.row, column=1, sticky="w")

        Button(self.right_frame, text="修改", command=self.submit) \
            .grid(row=row.row, column=2, sticky="w", pady=self.ui.pady_upper)

    def _all_selected(self, _type: str):
        for value in self.keys.values():
            getattr(self, value[0]).set(_type)

    def submit(self, event=None):
        try:
            self.reg()
            msg = "重启文件管理器后生效！"
            self.ui.show_msg(msg)
            logger.info(msg)
        except Exception as e:
            self.ui.show_msg(str(e), "error")
            logger.error(e)
